package com.icecloud.cloud.upms.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.icecloud.cloud.upms.common.utils.IpUtils;
import com.icecloud.cloud.upms.common.utils.RestfulResponseBody;
import com.icecloud.cloud.upms.entity.Role;
import com.icecloud.cloud.upms.service.RoleService;

/**
 * 测试核心用户
 * 
 * @author penghaozhong
 *
 */
@RestController
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	/**
	 * 新增一个角色
	 * 
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value = "/role", method = RequestMethod.POST)
	public ResponseEntity<RestfulResponseBody> save(@RequestBody Role role) {
		RestfulResponseBody body = new RestfulResponseBody();
		try {
			roleService.save(role);
			body.setReason("成功");
			body.setResultFlag(1);
		} catch (DataAccessException e) {
			body.setReason("保存失败：角色名称已存在！");
			body.setResultFlag(8012);
		}

		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}
	
	@RequestMapping("/roles")
	public ResponseEntity<RestfulResponseBody> list(String roleName,@RequestParam(defaultValue="1") int pageNum, @RequestParam(defaultValue="4") int pageSize) {
		RestfulResponseBody body = new RestfulResponseBody();
		body.put("data", roleService.list(roleName, pageNum, pageSize));
		body.setReason("成功");
		body.setResultFlag(1);
		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}
	
}
